package com.smxy.mall.controller;

import com.smxy.mall.common.Response;
import com.smxy.mall.entity.Shop;
import com.smxy.mall.model.request.PageReq;
import com.smxy.mall.model.request.ShopReq;
import com.smxy.mall.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "店铺管理")
@RestController
@RequestMapping("/shop")
public class ShopController {
    @Resource
    private ShopService shopService;

    @ApiOperation("店铺列表")
    @GetMapping("/list")
    public Object list() {
        return Response.success(shopService.list());
    }

    @ApiOperation("店铺列表(分页)")
    @GetMapping("/listPage")
    public Object listPage(@Valid PageReq pageReq) {
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        Pageable pageable = PageRequest.of((pageReq.getPage()-1),pageReq.getSize(),sort);
        Page<Shop> list = shopService.listPage(pageable);
        return Response.success(list);
    }

    @ApiOperation("店铺待审核列表（分页）")
    @GetMapping("/checkList")
    public Object checkList(@Valid PageReq pageReq) {
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of((pageReq.getPage()-1),pageReq.getSize(),sort);
        return Response.success(shopService.listStatusPage(2,pageable));
    }

    @ApiOperation("店铺待审核列表（分页）")
    @GetMapping("/successList")
    public Object successList(@Valid PageReq pageReq) {
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of((pageReq.getPage()-1),pageReq.getSize(),sort);
        return Response.success(shopService.listStatusPage(1,pageable));
    }

    @ApiOperation("新增/编辑 店铺")
    @PostMapping("/save")
    public Object save(ShopReq shopReq) {
        return Response.success(shopService.save(shopReq));
    }

    @ApiOperation("店铺信息")
    @GetMapping("/shopInfo")
    public Object shopInfo(@RequestParam("id")Integer id) {
        return Response.success(shopService.info(id));
    }

}
